<script lang="ts" setup>
import { computed, watch, nextTick, ref } from 'vue'
import { ArrowUp, ArrowDown } from '@element-plus/icons-vue'

import { ScrollLeft } from '@/hook/domHandler'
import { useSongStoreHook } from '@/store/modules/song'
import { useUserStoreHook } from '@/store/modules/user'

const songStore = useSongStoreHook()
const userStore = useUserStoreHook()

const currentSongDetail = computed(() => songStore.currentSongDetail)
const ids = computed(() => userStore.likeIds)
const pos = ref('160px')
watch(
  () => currentSongDetail.value.al,
  (newValue) => {
    if (newValue && newValue.name) {
      nextTick(() => {
        let beginRef = document.querySelector('.scroll_begin') as HTMLDivElement
        let endRef = document.querySelector('.scroll_end') as HTMLDivElement
        let scrollRef = document.querySelector(
          '.title-transition'
        ) as HTMLDivElement
        if (beginRef.offsetWidth > 170) {
          pos.value = '160px'
        } else {
          pos.value = beginRef.offsetWidth - 10 + 'px'
        }
        new ScrollLeft(scrollRef, beginRef, endRef)
      })
    }
  },
  {
    deep: true
  }
)

// 是否打开歌词弹窗
const open = computed(() => songStore.lyricOpen)

const changeOpen = (Flag: boolean) => {
  songStore.lyricOpen = Flag
}

const xihuan = () => {
  userStore.likeChange(currentSongDetail.value)
}
</script>

<template>
  <div
    class="detail_container"
    :key="currentSongDetail.id"
    :class="{ open_lyric: !open }"
  >
    <div class="detail_toggle">
      <div class="detail_image" @click="changeOpen(false)">
        <el-icon class="arrow_down"><ArrowDown /></el-icon>
      </div>
      <div class="icon_box">
        <i
          class="icon-border"
          :class="
            ids.includes(currentSongDetail.id)
              ? 'icon-xindong-'
              : 'icon-xindong-1'
          "
          @click="xihuan"
        ></i>
        <i class="icon-tianjia icon-border"></i>
      </div>
    </div>
    <div class="detail_toggle">
      <div class="detail_image" @click="changeOpen(true)">
        <el-icon class="arrow_up"><ArrowUp /></el-icon>
        <img
          v-if="currentSongDetail.al"
          :src="currentSongDetail.al?.picUrl"
          alt=""
        />
      </div>
      <div class="detail_textarea">
        <i
          :class="
            ids.includes(currentSongDetail.id)
              ? 'icon-xindong-'
              : 'icon-xindong-1'
          "
          @click="xihuan"
        ></i>
        <div class="title-transition" @click="changeOpen(false)">
          <span class="scroll_begin">
            <span>{{ currentSongDetail.name }}</span>
            <span
              v-if="currentSongDetail?.tns"
              style="color: var(--music-color-7)"
            >
              ({{ currentSongDetail?.tns[0] }})
            </span>
          </span>
          <span class="scroll_end"></span>
        </div>
        <div class="title-artist">
          <LineArtists width="100%" :artists="currentSongDetail?.ar" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.detail_container {
  width: 30%;
  min-width: 200px;
  transition: all 0.4s linear;

  .detail_toggle {
    display: flex;
    width: 100%;
    height: 80px;
    padding: 10px 0;
    line-height: 60px;
  }
  .icon_box {
    margin-left: 40px;
    height: 60px;
    display: flex;
    align-items: center;
    .icon-border {
      padding: 6px;
      border: 1px solid var(--music-color-4);
      border-radius: 50%;
      color: var(--music-recommend-text-color);
      &:hover {
        background-color: var(--music-color-1);
      }
    }
  }
}

.open_lyric {
  transform: translateY(-80px);
}
.detail_image {
  width: 60px;
  height: 60px;
  overflow: hidden;
  border-radius: 10px;
  position: relative;
  cursor: pointer;

  .arrow_up,
  .arrow_down {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 28px;
    z-index: 10;
    opacity: 0;
  }
  .arrow_down {
    opacity: 1;
  }
  &:hover {
    img {
      filter: brightness(40%) blur(2px);
    }
    .arrow_up {
      opacity: 1;
    }
  }
}

.detail_textarea {
  height: 60px;
  margin-left: 20px;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  position: relative;
  padding-right: 30px;
  .icon-xindong-1,
  .icon-xindong- {
    position: absolute;
    left: v-bind('pos');
    z-index: 99;
    top: 25%;
    transform: translateY(-50%);
  }
}

.icon-xindong-,
.icon-xindong-1,
.icon-tianjia {
  font-size: 26px;
  margin-left: 10px;
  cursor: pointer;
}
.icon-xindong- {
  color: $red;
}
.icon-xindong-1,
.icon-itianjia1 {
  color: var(--music-link-color);
  &:hover {
    color: var(--music-link-hover-color);
  }
}

.title-transition,
.title-artist {
  display: inline-block;
  width: auto;
  max-width: 170px;
  overflow: hidden;
  white-space: nowrap;
  line-height: 30px;
  height: 30px;
  position: relative;
  .scroll_begin,
  .scroll_end {
    padding-right: 10px;
    color: var(--music-text-link-color);
  }
}
</style>
